package com.dgut.usermodule.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.usermodule.dao.PermJapRepository;
import com.dgut.usermodule.entity.Permission;
@Service
@Transactional
public class PerServiceImpl implements PermService{
	@Autowired
	private PermJapRepository dao;
	@Override
	public boolean delete(Long id) {
		if (dao.findOne(id)!=null) {
			dao.delete(id);
			return true;
		}
		return false;
	}
	//分页查询
	@Override
	public Page<Permission> findAll(Specification<Permission> spec, Pageable pageable) {
		return dao.findAll(spec, pageable);
	}
	//授权
	@Override
	public int authorize(Long userId, Long permId) {
		return dao.authorize(userId, permId);
	}
	//撤权
	@Override
	public int cacelauthorize(Long userId, Long permId) {
		return dao.cacelauthorize(userId, permId);
	}
	@Override
	public List<Permission> list() {
		return dao.findAll();
	}
	@Override
	public Permission findOne(Long id) {
		return dao.findOne(id);
	}
	@Override
	public boolean save(Permission perm) {
		if (dao.save(perm)!=null) {
			return true;
		}
		return false;
	}
	@Override
	public boolean update(Permission perm) {
		if (dao.save(perm)!=null) {
			return true;
		}
		return false;
	}
	@Override
	public void reauthorize(Long userId, Long[] permId) {
		dao.deleteauthorize(userId);
		for(Long pid:permId) {
			dao.authorize(userId, pid);
		}
	}

	

}
